package com.ruoyi.business.controller;

import com.ruoyi.business.domain.TemplateFile;
import com.ruoyi.business.domain.bo.TemplateFileBo;
import com.ruoyi.business.domain.vo.TemplateFileVo;
import com.ruoyi.business.service.ITemplateFileService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysOssService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 最爱喝兽奶
 * @create 2022/9/7
 **/
@RestController
@RequestMapping("/business/templateFile")
public class TemplateFileController extends BaseController {

    @Autowired
    private ITemplateFileService templateFileService;

    @Autowired
    private ISysOssService ossService;

    @ApiOperation("查询模板文件的列表")
    @GetMapping("/list")
    public AjaxResult list(TemplateFileBo bo) {
        TableDataInfo<TemplateFileVo> tableDataInfo = templateFileService.queryListByPage(bo);
        return AjaxResult.success(tableDataInfo);
    }

    @ApiOperation("查询模板文件")
    @GetMapping("/{id}")
    public AjaxResult getTemplateFile(@PathVariable("id") String id) {
        TemplateFileVo vo = templateFileService.getTemplateFile(id);
        return AjaxResult.success(vo);
    }

    @ApiOperation("新增模板文件")
    @PostMapping
    public AjaxResult add(@RequestBody TemplateFileBo bo) {
        templateFileService.insertTemplateFile(bo);
        return AjaxResult.success();
    }

    @ApiOperation("修改模板文件")
    @PutMapping
    public AjaxResult edit(@RequestBody TemplateFileBo bo) {
        templateFileService.updateTemplateFile(bo);
        return AjaxResult.success();
    }

    @ApiOperation("删除模板文件")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        templateFileService.deleteTemplateFile(id);
        return AjaxResult.success();
    }

    @ApiOperation("下载模板文件")
    @GetMapping("/download")
    public void downloadTemplateFile(@RequestParam(value = "templateFile", required = true) String templateFile,
                                     HttpServletResponse response) throws IOException {
        byte[] data = ossService.download(templateFile);
        genCode(response, data);
    }

    /**
     * 生成文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException
    {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"templateFile.xls\"");
//        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

}
